//
//  TTHomeVcModel.m
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/25.
//  Copyright © 2016年 陶路路. All rights reserved.
//

#import "TTHomeVcModel.h"
#import "TTNetworkingTools.h"
#import "TTWelcomeModel.h"
#import "TTHomeModel.h"
#import "TTHomeCellModel.h"
@implementation TTHomeVcModel



-(void)getHomeData:(BOOL)isUping sucess:(void(^)(BOOL isSuccess,NSInteger count))success{
    
    long long maxId = 0;
    long long sinceId = 0;
    
    if (isUping) {
        
        TTHomeCellModel * model = self.dataArray.lastObject;
        
        maxId = model.cellModel.id;
        
        if (maxId > 0) {
            
            maxId -= 1;
        }
        
    }else{
        
        TTHomeCellModel * model = self.dataArray.firstObject;
        
        sinceId = model.cellModel.id;
    }
    
    [[TTNetworkingTools sharedManager] loadHomeData:[TTWelcomeModel sharedManager].model since_id:sinceId max_id:maxId sucessBlock:^(NSDictionary * responseObject) {
        
        NSMutableArray * array = [NSMutableArray array];
                                  
        NSArray * responseArray = responseObject[@"statuses"];
        
        for (NSDictionary * dict in responseArray) {
            
            TTHomeModel * model =  [[TTHomeModel alloc]initWithDictionary:dict];
            
            TTHomeCellModel * cellModel = [[TTHomeCellModel alloc]initWithModel:model];
            
            [array addObject:cellModel];
        }
        
        if (isUping) {
            
            self.dataArray = [self.dataArray arrayByAddingObjectsFromArray:array.copy];
            
            success(YES,-1);
            
        }else{
            
            self.dataArray = [array arrayByAddingObjectsFromArray:self.dataArray];
            
             success(YES,array.count);
        }

    } error:^(NSError *error) {
        
        success(NO,0);
        
    }];
}

@end